<html>
<head><meta charset="utf-8"><title>Getting the Span of a definition from a Place or Location · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html">Getting the Span of a definition from a Place or Location</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223027848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223027848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223027848">(Jan 17 2021 at 06:37)</a>:</h4>
<p>I have defined a closure earlier and later get an error when calling it, because I have not declared it as mutable. When compiling such code, in the function <code>report_mutability_error</code> in <code>rustc_mir::borrow_check::diagnostics::mutability_errors</code>, the error occurs when calling the closure, and not the definition of the closure, so I only have a <code>Place</code> or <code>Location</code> of the call. How do I refer to the <code>Span</code> of the definition from a <code>Place</code>or <code>Location</code>?</p>



<a name="223029820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223029820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223029820">(Jan 17 2021 at 07:35)</a>:</h4>
<p>I've made some progress, I think. <code>self.body.local_decls</code> has the definition of locals. I'm not sure which index I should use for the definition.</p>



<a name="223030039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030039">(Jan 17 2021 at 07:41)</a>:</h4>
<p>I have a PR up to fix such an issue</p>



<a name="223030097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030097">(Jan 17 2021 at 07:42)</a>:</h4>
<p>Is the issue <a href="https://github.com/rust-lang/rust/issues/80313">https://github.com/rust-lang/rust/issues/80313</a> ?</p>



<a name="223030117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030117">(Jan 17 2021 at 07:43)</a>:</h4>
<p>Oh I misunderstood the issue, sorry</p>



<a name="223030250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030250">(Jan 17 2021 at 07:46)</a>:</h4>
<p><del>So within the context of a closure the closure itself is refered to use Local::new(1)</del></p>



<a name="223030260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030260">(Jan 17 2021 at 07:47)</a>:</h4>
<p><del>Well, the desugared closure struct is referred to as Local::new(1)</del></p>



<a name="223030377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030377">(Jan 17 2021 at 07:50)</a>:</h4>
<p>Okay I think I understand</p>



<a name="223030500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030500">(Jan 17 2021 at 07:55)</a>:</h4>
<p>Does <code>self.body.source_info(location).span</code> do what you are looking for?</p>



<a name="223030800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223030800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223030800">(Jan 17 2021 at 08:03)</a>:</h4>
<p>I think that is the span of the call, not the definition, unfortunately.</p>



<a name="223046938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223046938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223046938">(Jan 17 2021 at 15:06)</a>:</h4>
<p>I think from a <code>Local</code> you can use the <code>local_decls</code> to get the <code>LocalDecl</code> and then <code>decl.source_info.span</code>?</p>



<a name="223047909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223047909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223047909">(Jan 17 2021 at 15:31)</a>:</h4>
<p><span class="user-mention" data-user-id="255061">@Léo Lanteri Thauvin</span>  Yes, but how do I obtain the <code>Local</code>of the definition, not the call?</p>



<a name="223049538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223049538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223049538">(Jan 17 2021 at 16:01)</a>:</h4>
<p>Actually, with mutation, "the" definition doesn't really make sense, as there could be multiple locations a variable is set.</p>



<a name="223049594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223049594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223049594">(Jan 17 2021 at 16:02)</a>:</h4>
<p>How would that work for closures from other crates? In that case the HIR is not available and the MIR likely isn't either.</p>



<a name="223049759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223049759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223049759">(Jan 17 2021 at 16:05)</a>:</h4>
<p>I don't understand what you mean by from other crates. In my example <a href="https://github.com/rust-lang/rust/issues/80313">https://github.com/rust-lang/rust/issues/80313</a> , the closure is a local variable.</p>



<a name="223049778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223049778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223049778">(Jan 17 2021 at 16:05)</a>:</h4>
<p>Never mind, got confused.</p>



<a name="223050336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223050336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223050336">(Jan 17 2021 at 16:16)</a>:</h4>
<p>I suppose if you passed the closure in as a parameter, then the parameter would have to be explicitly declared <code>FnMut</code>, which would give an appropriate error, while in my case, the closure is inferred as a <code>FnMut</code>, which can't be called when bound immutably, and that the problem only arises when calling it, not defining it, so I somehow need to recover the locations where it could be defined.</p>



<a name="223051066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223051066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kevin Tran <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223051066">(Jan 17 2021 at 16:30)</a>:</h4>
<p>Actually, since different closures have different types, there should only be one possible location where a variable with a closure can be defined, unless you do some shenanigans with <code>Box</code>or trait objects, but I think those require type annotations with <code>FnMut</code>, so they shouldn't be a problem</p>



<a name="223055660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20the%20Span%20of%20a%20definition%20from%20a%20Place%20or%20Location/near/223055660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20the.20Span.20of.20a.20definition.20from.20a.20Place.20or.20Location.html#223055660">(Jan 17 2021 at 18:14)</a>:</h4>
<p>I'm not sure what your approach to this problem is, but what I'd suggest you can try to see if the type of Place <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs#L225-L244">here</a> is a closure. </p>
<p>And get the DefId of the closure/generator. </p>
<p>This would be something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">Place</span>::<span class="n">ty_from</span><span class="p">(</span><span class="n">local</span><span class="p">,</span><span class="w"> </span><span class="n">proj_base</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">infcx</span><span class="p">.</span><span class="n">tcx</span><span class="p">).</span><span class="n">ty</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">ty</span>::<span class="n">Closure</span><span class="p">(</span><span class="n">def_id</span><span class="p">,</span><span class="w"> </span><span class="o">..</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">ty</span>::<span class="n">Generator</span><span class="p">(</span><span class="n">def_id</span><span class="p">,</span><span class="w"> </span><span class="o">..</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>and then once you have the def_id you can go through the list of captured places for the closure and see if a place is captured by a MutableBorrow, so something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">mut_borrow_iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="w"></span>
<span class="w">      </span><span class="p">.</span><span class="n">infcx</span><span class="w"></span>
<span class="w">      </span><span class="p">.</span><span class="n">tcx</span><span class="w"></span>
<span class="w">      </span><span class="p">.</span><span class="n">typeck</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="p">.</span><span class="n">closure_min_captures_flattened</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">captured_place</span><span class="o">|</span><span class="w">  </span><span class="fm">matches!</span><span class="p">(</span><span class="n">captured_place</span><span class="p">.</span><span class="n">info</span><span class="p">.</span><span class="n">capture_kind</span><span class="p">,</span><span class="w"> </span><span class="n">ty</span>::<span class="n">UpvarBorrow</span>::<span class="n">MutableBorrow</span><span class="p">(</span><span class="n">_</span><span class="p">));</span><span class="w"></span>
</code></pre></div>
<p>You can then access the span for this captured_place using <code>tcx.hir().span(captured_place.info.expr_id)</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>